<?php
	class Account extends CI_Controller {
		public function index() {
			if($this->session->userdata('character')) {
				redirect('character/');
			}
			
			$account = $this->session->userdata('account');
			$this->session->all_userdata();
			$data['title'] = 'RoBattle - Account Index';
			$data['loggedChar'] = false;
			$data['logged'] = true;
			$data['admin'] = $account->admin == 1 ? true : false;
			$data['page'] = 'account';
			
			$this->load->view('templates/header', $data);
			$this->load->view('templates/links-wo-js');
			$this->load->view('templates/menu');
			$this->load->view('account/index');
			$this->load->view('templates/extra');
			$this->load->view('templates/footer');
		}
		
		public function logout() {
			$this->session->unset_userdata('userId');
			redirect('pages/index');
		}
		
		public function profile() {
			if($this->session->userdata('character')) {
				redirect('character/');
			}
			
			$account = $this->session->userdata('account');
			$data['title'] = 'RoBattle - Account Profile';
			$data['loggedChar'] = false;
			$data['logged'] = true;
			$data['admin'] = $account->admin == 1 ? true : false;
			$data['page'] = 'account';
			$data['account'] = $this->Account_model->getAccountStats($this->session->userdata('userId'));
			
			$this->load->view('templates/header', $data);
			$this->load->view('templates/links-wo-js');
			$this->load->view('templates/menu');
			$this->load->view('account/profile');
			$this->load->view('templates/extra');
			$this->load->view('templates/footer');
		}
		
		public function change_password() {
			if($this->session->userdata('character')) {
				redirect('character/');
			}
			
			$account = $this->session->userdata('account');
			$data['title'] = 'RoBattle - Change Password';
			$data['loggedChar'] = false;
			$data['logged'] = true;
			$data['admin'] = $account->admin == 1 ? true : false;
			$data['page'] = 'account';
			
			$this->load->view('templates/header', $data);
			$this->load->view('templates/links-wo-js');
			$this->load->view('templates/menu');
			
			$rules = array(
				array(
					'field' => 'old_password',
					'label' => 'Old password',
					'rules' => 'required'
				),
				array(
					'field' => 'new_password',
					'label' => 'New password',
					'rules' => 'trim|required|min_length[4]|max_length[16]|matches[confirm_password]'
				),
				array(
					'field' => 'confirm_password',
					'label' => 'Confirm password',
					'rules' => 'trim|required'
				)
			);
			
			$this->form_validation->set_rules($rules);
			$this->form_validation->set_error_delimiters('<em>','</em>');
			
			if($this->form_validation->run()) {
				$oldPassword = $this->input->post('old_password');
				$newPassword = $this->input->post('new_password');
				
				if($this->Account_model->verifyPassword($oldPassword, $this->session->userdata('userId'))) {
					if($oldPassword != $newPassword) {
						$this->Account_model->updateUserPassword($newPassword, $this->session->userdata('userId'));
						$data['message'] = 'You successfully changed your password.';
						$this->load->view('account/change-password', $data);
					} else {
						$data['error'] = 'Your new password can\'t be the same as the old one.';
						$this->load->view('account/change-password', $data);
					}
				} else {
					$data['error'] = 'Your old password is incorrect.';
					$this->load->view('account/change-password', $data);
				}
			} else {
				$this->load->view('account/change-password');
			}
			
			$this->load->view('templates/extra');
			$this->load->view('templates/footer');
		}
		
		public function create_character() {
			if($this->session->userdata('character')) {
				redirect('character/');
			}
			
			$account = $this->session->userdata('account');
			$data['title'] = 'RoBattle - Create Character';
			$data['loggedChar'] = false;
			$data['logged'] = true;
			$data['admin'] = $account->admin == 1 ? true : false;
			$data['page'] = 'account';
			
			$this->load->view('templates/header', $data);
			$this->load->view('templates/links-wo-js');
			$this->load->view('templates/menu');
			
			$rules = array(
				array(
					'field' => 'name',
					'label' => 'Name',
					'rules' => 'required|xss_clean'
				),
				array(
					'field' => 'class',
					'label' => 'Class',
					'rules' => 'required'
				)
			);
			
			$this->form_validation->set_rules($rules);
			$this->form_validation->set_error_delimiters('<em>','</em>');
			
			if($account['characters'] == $account['max_characters']) {
				$data['message'] = 'You already have the maximum allowed number of characters.';
				$this->load->view('account/create-character', $data);
			} elseif($this->form_validation->run()) {
				$name = $this->input->post('name');
				$class = $this->input->post('class');
				
				if($this->Account_model->verifyName($name)) {
					$this->Account_model->createCharacter($name, $class, $this->session->userdata('userId'));
					$this->Account_model->updateUserCharacters($this->session->userdata('userId'));
					$data['message'] = 'Your character has been created.';
					$this->load->view('account/create-character', $data);
				} else {
					$data['error'] = 'That name is already in use.';
					$this->load->view('account/create-character', $data);
				}
			} else {
				$this->load->view('account/create-character');
			}
			
			$this->load->view('templates/extra');
			$this->load->view('templates/footer');
		}
		
		public function edit_profile() {
			if($this->session->userdata('character')) {
				redirect('character/');
			}
			
			$account = $this->session->userdata('account');
			$data['title'] = 'RoBattle - Edit Profile';
			$data['loggedChar'] = false;
			$data['logged'] = true;
			$data['admin'] = $account->admin == 1 ? true : false;
			$data['page'] = 'account';
			$data['days'] = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31');
			$data['months'] = array('January','February','March','April','May','June','July','August','September','October','November','December');
			$data['years'] = array('1950','1951','1952','1953','1954','1955','1956','1957','1958','1959','1960','1961','1962','1963','1964','1965','1966','1967','1968','1969','1970','1971','1972','1973','1974','1975','1976','1977','1978','1979','1980','1981','1982','1983','1984','1985','1986','1987','1988','1989','1990','1991','1992');
			$data['countries'] = array(
				"Afghanistan",
				"Albania",
				"Algeria",
				"Andorra",
				"Angola",
				"Antigua and Barbuda",
				"Argentina",
				"Armenia",
				"Australia",
				"Austria",
				"Azerbaijan",
				"Bahamas",
				"Bahrain",
				"Bangladesh",
				"Barbados",
				"Belarus",
				"Belgium",
				"Belize",
				"Benin",
				"Bhutan",
				"Bolivia",
				"Bosnia and Herzegovina",
				"Botswana",
				"Brazil",
				"Brunei",
				"Bulgaria",
				"Burkina Faso",
				"Burundi",
				"Cambodia",
				"Cameroon",
				"Canada",
				"Cape Verde",
				"Central African Republic",
				"Chad",
				"Chile",
				"China",
				"Colombi",
				"Comoros",
				"Congo (Brazzaville)",
				"Congo",
				"Costa Rica",
				"Cote d'Ivoire",
				"Croatia",
				"Cuba",
				"Cyprus",
				"Czech Republic",
				"Denmark",
				"Djibouti",
				"Dominica",
				"Dominican Republic",
				"East Timor (Timor Timur)",
				"Ecuador",
				"Egypt",
				"El Salvador",
				"Equatorial Guinea",
				"Eritrea",
				"Estonia",
				"Ethiopia",
				"Fiji",
				"Finland",
				"France",
				"Gabon",
				"Gambia, The",
				"Georgia",
				"Germany",
				"Ghana",
				"Greece",
				"Grenada",
				"Guatemala",
				"Guinea",
				"Guinea-Bissau",
				"Guyana",
				"Haiti",
				"Honduras",
				"Hungary",
				"Iceland",
				"India",
				"Indonesia",
				"Iran",
				"Iraq",
				"Ireland",
				"Israel",
				"Italy",
				"Jamaica",
				"Japan",
				"Jordan",
				"Kazakhstan",
				"Kenya",
				"Kiribati",
				"Korea, North",
				"Korea, South",
				"Kuwait",
				"Kyrgyzstan",
				"Laos",
				"Latvia",
				"Lebanon",
				"Lesotho",
				"Liberia",
				"Libya",
				"Liechtenstein",
				"Lithuania",
				"Luxembourg",
				"Macedonia",
				"Madagascar",
				"Malawi",
				"Malaysia",
				"Maldives",
				"Mali",
				"Malta",
				"Marshall Islands",
				"Mauritania",
				"Mauritius",
				"Mexico",
				"Micronesia",
				"Moldova",
				"Monaco",
				"Mongolia",
				"Morocco",
				"Mozambique",
				"Myanmar",
				"Namibia",
				"Nauru",
				"Nepa",
				"Netherlands",
				"New Zealand",
				"Nicaragua",
				"Niger",
				"Nigeria",
				"Norway",
				"Oman",
				"Pakistan",
				"Palau",
				"Panama",
				"Papua New Guinea",
				"Paraguay",
				"Peru",
				"Philippines",
				"Poland",
				"Portugal",
				"Qatar",
				"Romania",
				"Russia",
				"Rwanda",
				"Saint Kitts and Nevis",
				"Saint Lucia",
				"Saint Vincent",
				"Samoa",
				"San Marino",
				"Sao Tome and Principe",
				"Saudi Arabia",
				"Senegal",
				"Serbia and Montenegro",
				"Seychelles",
				"Sierra Leone",
				"Singapore",
				"Slovakia",
				"Slovenia",
				"Solomon Islands",
				"Somalia",
				"South Africa",
				"Spain",
				"Sri Lanka",
				"Sudan",
				"Suriname",
				"Swaziland",
				"Sweden",
				"Switzerland",
				"Syria",
				"Taiwan",
				"Tajikistan",
				"Tanzania",
				"Thailand",
				"Togo",
				"Tonga",
				"Trinidad and Tobago",
				"Tunisia",
				"Turkey",
				"Turkmenistan",
				"Tuvalu",
				"Uganda",
				"Ukraine",
				"United Arab Emirates",
				"United Kingdom",
				"United States",
				"Uruguay",
				"Uzbekistan",
				"Vanuatu",
				"Vatican City",
				"Venezuela",
				"Vietnam",
				"Yemen",
				"Zambia",
				"Zimbabwe"
			);
			$rules = array(
				array(
					'field' => 'fname',
					'label' => 'First Name',
					'rules' => 'required|xss_clean'
				),
				array(
					'field' => 'lname',
					'label' => 'Last Name',
					'rules' => 'required|xss_clean'
				),
				array(
					'field' => 'nickname',
					'label' => 'Nickname',
					'rules' => 'required|xss_clean'
				),
				array(
					'field' => 'country',
					'label' => 'Country',
					'rules' => 'required'
				),
				array(
					'field' => 'city',
					'label' => 'City',
					'rules' => 'required|xss_clean'
				)
			);

			$this->form_validation->set_rules($rules);
			$this->form_validation->set_error_delimiters('<em>','</em>');
			
			$this->load->view('templates/header', $data);
			$this->load->view('templates/links-wo-js');
			$this->load->view('templates/menu');
			
			if($this->form_validation->run()) {
				$info = array(
					'fname' => $this->input->post('fname'),
					'lname' => $this->input->post('lname'),
					'nickname' => $this->input->post('nickname'),
					'birthdate' => $this->input->post('day') . '-' . $this->input->post('month') . '-' . $this->input->post('year'),
					'city' => $this->input->post('city'),
					'country' => $this->input->post('country')
				);
				
				$this->Account_model->updateUserStats($info, $this->session->userdata('userId'));
				$data['message'] = 'Your account info has been updated.';
				$this->load->view('account/edit-profile', $data);
			} else {
				$this->load->view('account/edit-profile');
			}
			
			$this->load->view('templates/extra');
			$this->load->view('templates/footer');
		}
		
		public function manage_characters() {
			if($this->session->userdata('character')) {
				redirect('character/');
			}
			
			$account = $this->session->userdata('account');
			$data['title'] = 'RoBattle - Manage Characters';
			$data['loggedChar'] = false;
			$data['logged'] = true;
			$data['admin'] = $account->admin == 1 ? true : false;
			$data['page'] = 'account';
			$data['characters'] = $this->Account_model->getAccountCharacters($this->session->userdata('userId'));
			
			$account = $this->Account_model->getAccountStats($this->session->userdata('userId'));
			
			$this->load->view('templates/header', $data);
			$this->load->view('templates/links-w-js');
			$this->load->view('templates/menu');
			
			$charId = $this->input->post('charId');
			
			if($this->input->get_post('action') == 'Delete') {
				$this->Account_model->deleteCharacter($charId, $this->session->userdata('userId'));
				$data['message'] = 'Your character has been deleted.';
				$this->load->view('account/manage-characters', $data);
			}
			if($this->input->get_post('action') == 'Login') {
				$this->session->set_userdata('charId', $charId);
				redirect('character');
			} else {
				$this->load->view('account/manage-characters');
			}
			
			$this->load->view('templates/extra');
			$this->load->view('templates/footer');
		}
	}
?>